<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * Class ConfigController
 * @package Admin\Controller
 * 2018-04-11   商家设置
 */
class ConfigController extends AdminBasicController{

    public function _initialize(){
        $this->checkLogin();
        $pro = M("areas")->where(array('parent_id'=>1))->order('sort_order DESC,area_id ASC')->select();
        $this->assign("pro",$pro);
    }

    /**
     * 商家信息
     * @author mss
     * @time 2018-04-11
     */
    public function config(){
        if(IS_POST){
            $id = $_POST['id'];
            if($id){
                $res = M('Config')->where(['id'=>$id])->save($_POST);
            }else{
                $res = M('Config')->add($_POST);
            }
            if($res){
                $this->success('保存成功');
            }else{
                apiResponse('保存失败');
            }
        }else{
            $config = M('Config')->limit(1)->find();
            $this->assign('config',$config);
            if($config['province']){
                $city = M('Areas')->where(['parent_id'=>$config['province']])->field('area_id,area_name')->select();
                $this->assign('city',$city);
            }
            if($config['city']){
                $areas = M('Areas')->where(['parent_id'=>$config['city']])->field('area_id,area_name')->select();
                $this->assign('area',$areas);
            }

            $this->display('config');
        }
    }


    /**ajax上传图片*/
    public function uploadPic(){
        $pic       = $_POST['pic'];
        $pic_name      = $_POST['pic_name'];
        $temp = explode('.',$pic_name);
        $ext = uniqid().'.'.end($temp);
        $base64    = substr(strstr($pic, ","), 1);
        $image_res = base64_decode($base64);
        $pic_link  = "Uploads/Shop/".date('Y-m-d').'/'.$ext;
        $saveRoot = "Uploads/Shop/".date('Y-m-d').'/';
        /**检查目录是否存在  循环创建目录*/
        if(!is_dir($saveRoot)){
            mkdir($saveRoot, 0777, true);
        }
        $res = file_put_contents($pic_link ,$image_res);
        if($res){
            $ajaxData = array("flag" => "success", "message"=>"上传成功！" );
            $result_data['path'] = $pic_link;
            $ajaxData['data'] = $result_data;
            $this->ajaxReturn(json_encode($ajaxData));
        }else{
            $ajaxData = array("flag" => "error", "message"=>"上传失败","data" => array());
            $this->ajaxReturn(json_encode($ajaxData));
        }
    }

    /**删除上传的相册的图片*/
    public function delPhoto(){
        /**判断删除的商家的图片type:1头像 type2商家展示图 3商家的资质证明图*/

        $w['id'] = $_POST['id'];
        $res = M("Config")->where($w)->limit(1)->find();
        $pic = "";
        if($_POST['type'] == 1){
            $data['pic'] = "";
            $pic = M("Config")->where($w)->limit(1)->save($data);
        }else{
            $pic = 1;
        }

        $file  = $_POST['file_path'];
        $result = @unlink($file);
        if ($result == false && empty($pic)) {
            $this->ajaxReturn(0);
        } else {
            $this->ajaxReturn(1);
        }
    }


    /**
     * ajax获取城市
     */
    public function ajaxCity(){
        $where["parent_id"] = $_GET['parent_id'];
        $data = M("areas")->where($where)->select();
        echo json_encode($data);
    }
    /**
     * ajax获取区域
     */
    public function ajaxArea(){
        $where["parent_id"] = $_GET['parent_id'];
        $data = M("areas")->where($where)->select();
        echo json_encode($data);
    }


}